<template>
  <Popup v-model="show" position="bottom">
    <Picker
      show-toolbar
      value-key="codeName"
      :loading="isLoading"
      :columns="dataRes"
      @confirm="onConfirmClick"
      @cancel="onCancelClick"
    />
  </Popup>
</template>

<script>
import Popup from '@/widgets/Popup'
import Picker from '@/widgets/Picker'
import { resTol } from '@/tools/public'

export default {
  name: 'CodeDataPickerCpt',
  components: {
    Popup,
    Picker
  },
  props: {
    codeType: {
      type: [String],
      default: ''
    }
  },
  data() {
    return {
      show: false,
      isLoading: false,
      dataType: this.codeType,
      dataRes: []
    }
  },
  methods: {
    showView(dataType) {
      if (dataType) {
        this.dataType = dataType
      }
      this.show = true
      this.isLoading = true
      resTol.initReqCodeListResCb([this.dataType], res => {
        for (const code of res) {
          if (code.codeType === this.dataType) {
            this.dataRes = code.codeList
          }
        }
        this.isLoading = false
      })
    },
    onConfirmClick(val) {
      this.$emit('onSelectedChange', val, this.dataType)
      this.show = false
    },
    onCancelClick() {
      this.show = false
    }
  }
}
</script>
